#include "stdafx.h"
#include "ShaderShadowVolume.h"

#include "DrawCall.h"
#include "RenderingData.h"
#include "Model.h"
#include "Material.h"

#include "GraphicsResources.h"
#include "StateManager.h"

using namespace mgfx;

ShaderShadowVolume::ShaderShadowVolume()
{
	mVar_gWorld = NULL;
	mVar_gViewProj = NULL;
	mVar_gLightPos = NULL;
	mVar_gExtrudeBias = NULL;
	mVar_gExtrudeAmt = NULL;

	mFilename = L"ShadowVolume.fx";
}

bool ShaderShadowVolume::init()
{
	Tech = Effect->GetTechniqueByName("tech");
	mVar_gWorld = Effect->GetVariableByName("gWorld")->AsMatrix();
	mVar_gViewProj = Effect->GetVariableByName("gViewProj")->AsMatrix();
	mVar_gLightPos = Effect->GetVariableByName("gLightPos")->AsVector();
	mVar_gExtrudeBias = Effect->GetVariableByName("gExtrudeBias")->AsScalar();
	mVar_gExtrudeAmt = Effect->GetVariableByName("gExtrudeAmt")->AsScalar();

	RasterizerState = GraphicsResources::getGlobal()->getStateMgr()->getRasterizerState(MGFX_RS_NO_CULLING);
	DepthStencilState = GraphicsResources::getGlobal()->getStateMgr()->getDepthStencilState(MGFX_DSS_TWO_SIDED_STENCIL);
	StencilRef = 1;
	BlendState = GraphicsResources::getGlobal()->getStateMgr()->getBlendState(MGFX_BS_DISABLE_FRAMEBUFFER);
	
	if(!Tech || ! mVar_gWorld || !mVar_gViewProj)
		return false;
	else
	{
		mInitialized = true;
		return true;
	}
}

void ShaderShadowVolume::setParamsGlobal()
{
	RenderingData & data = RenderingData::Instance;
	mVar_gLightPos->SetFloatVector(data.Pointlight.Position);
	mVar_gExtrudeBias->SetFloat(0.1f);
	mVar_gExtrudeAmt->SetFloat(20.0f);
}

void ShaderShadowVolume::setParamsModel(ModelDrawCall * drawCall)
{
	//D3DXMATRIX wvp;
	//D3DXMatrixMultiply(&wvp, &drawCall->WorldMatrix, &RenderingData::Instance.ViewProj);

	mVar_gWorld->SetMatrix((float*)&drawCall->WorldMatrix);
	mVar_gViewProj->SetMatrix((float*)&RenderingData::Instance.ViewProj);
}